package com.ruoyix.flowable.controller;


import com.ruoyix.flowable.domain.vo.BpmTaskApproveVo;
import com.ruoyix.flowable.domain.vo.BpmTaskAssigneeVo;
import lombok.RequiredArgsConstructor;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.ruoyix.common.core.controller.BaseController;
import com.ruoyix.common.core.domain.PageQuery;
import com.ruoyix.common.core.domain.R;
import com.ruoyix.flowable.domain.vo.BpmTaskVo;
import com.ruoyix.flowable.domain.bo.BpmTaskBo;
import com.ruoyix.flowable.service.IBpmTaskService;
import com.ruoyix.common.core.page.TableDataInfo;

import java.util.List;

/**
 * 流程任务信息
 *
 * @author ruoyi
 * @date 2023-11-27
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/flowable/bpmTask")
public class BpmTaskController extends BaseController {

    private final IBpmTaskService iBpmTaskService;

    /**
     * 查询流程任务信息列表
     */
    @SaCheckPermission("flowable:bpmTask:list")
    @GetMapping("/list")
    public TableDataInfo<BpmTaskVo> list(BpmTaskBo bo, PageQuery pageQuery) {
        return iBpmTaskService.queryPageList(bo, pageQuery);
    }


    /**
     * 查询已完成流程任务列表
     */
    @SaCheckPermission("flowable:bpmTask:list")
    @GetMapping("/finish")
    public TableDataInfo<BpmTaskVo> finish(BpmTaskBo bo, PageQuery pageQuery) {
        return iBpmTaskService.finishList(bo, pageQuery);
    }

    /**
     * 审批通过节点
     * @param bpmTaskApproveVo
     * @return
     */
    @PostMapping("/complete")
    public R<Void> complete(@RequestBody BpmTaskApproveVo bpmTaskApproveVo){
        return toAjax(iBpmTaskService.complete(bpmTaskApproveVo));
    }


    /**
     * 审批驳回节点
     * @param bpmTaskApproveVo
     * @return
     */
    @PostMapping("/reject")
    public R<Void> reject(@RequestBody BpmTaskApproveVo bpmTaskApproveVo){
        return toAjax(iBpmTaskService.reject(bpmTaskApproveVo.getTaskId(),bpmTaskApproveVo.getFlowData().getComment()));
    }


    /**
     * 委派
     * @param bpmTaskApproveVo
     * @return
     */
    @PostMapping("/delegate")
    public R<Void> delegate(@RequestBody BpmTaskApproveVo bpmTaskApproveVo){
        return toAjax(iBpmTaskService.delegateTask(bpmTaskApproveVo));
    }


    /**
     * 转办
     * @param bpmTaskApproveVo
     * @return
     */
    @PostMapping("/transfer")
    public R<Void> transfer(@RequestBody BpmTaskApproveVo bpmTaskApproveVo){
        return toAjax(iBpmTaskService.transferTask(bpmTaskApproveVo));
    }


    /**
     * 获得下一节点信息
     * @param taskId
     * @return
     */
    @GetMapping("/getNextNodeMessage")
    public R<BpmTaskApproveVo> getNextNodeMessage(String taskId){
        return R.ok(iBpmTaskService.getNextNodeMessage(taskId));
    }


    @GetMapping("/getNodeList")
    public R<List<BpmTaskAssigneeVo>> getNodeList(String taskId){
        return R.ok(iBpmTaskService.getNodeList(taskId));
    }


    @PostMapping("/freeFlowTask")
    public R<Void>freeFlowTask(@RequestBody BpmTaskApproveVo bpmTaskApproveVo){
        return toAjax(iBpmTaskService.freeFlowTask(bpmTaskApproveVo));
    }

}
